//
//  CTCUserModel.m
//  biz
//
//  Created by 李小龙 on 2017/3/20.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "CTCUserModel.h"
#import "NetManager.h"
#import "SDWebImageTool.h"
#import "CTCUserDetailModel.h"

#define SavePath    [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"user.info"]

@implementation CTCUserModel
+ (instancetype)shareUser
{
    static CTCUserModel *_user;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _user = [CTCUserModel readFromLocal];
    });
    
    return _user;
}

+ (void)saveInfo
{
    [NSKeyedArchiver archiveRootObject:[CTCUserModel shareUser] toFile:SavePath];
}

+ (void)refresh
{
    CTCUserModel *user = [NSKeyedUnarchiver unarchiveObjectWithFile:SavePath];
    if (user != nil) {
        CTCUserModel *model = [CTCUserModel shareUser];
        model = [user copy];
    }
}

#pragma mark - RU
- (void)save2Local
{
    [NSKeyedArchiver archiveRootObject:self toFile:SavePath];
}

+ (instancetype)readFromLocal
{
    CTCUserModel *user = [NSKeyedUnarchiver unarchiveObjectWithFile:SavePath];
    
    if (nil == user) {
        user = [CTCUserModel new];
    }
    
    return user;
}


#pragma mark -

- (NSUInteger)hash {
    return [self yy_modelHash];
}
- (BOOL)isEqual:(id)object {
    return [self yy_modelIsEqual:object];
}
- (NSString *)description {
    return [self yy_modelDescription];
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeBool:_login forKey:@"login"];
    [aCoder encodeBool:_complete forKey:@"complete"];
    [aCoder encodeObject:_cookieID forKey:@"cookieID"];
//    [aCoder encodeObject:_name forKey:@"name"];
//    [aCoder encodeObject:_city forKey:@"city"];
//    [aCoder encodeObject:_uuid forKey:@"uuid"];
    [aCoder encodeObject:_detailModel forKey:@"detailModel"];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        _login = [aDecoder decodeBoolForKey:@"login"];
        _complete = [aDecoder decodeBoolForKey:@"complete"];
        _cookieID = [aDecoder decodeObjectForKey:@"cookieID"];
//        _name = [aDecoder decodeObjectForKey:@"name"];
//        _city = [aDecoder decodeObjectForKey:@"city"];
//        _uuid = [aDecoder decodeObjectForKey:@"uuid"];
        _detailModel = [aDecoder decodeObjectForKey:@"detailModel"];
    }
    
    return self;
}

- (id)copyWithZone:(NSZone *)zone
{
    CTCUserModel *copy = [[[self class] allocWithZone:zone] init];
    copy.login = self.login;
    copy.complete = self.complete;
    copy.cookieID = [self.cookieID copyWithZone:zone];
//    copy.name = [self.name copy];
//    copy.city = [self.city copy];
//    copy.uuid = [self.uuid copy];
    copy.detailModel = self.detailModel;
    return copy;
}


#pragma mark - set

- (void)setLogin:(BOOL)login{
    _login = login;
    [CTCUserModel saveInfo];
}


- (void)setIsComplete:(BOOL)complete{
    _complete = complete;
    [CTCUserModel saveInfo];
}

- (void)setCookieID:(NSString *)cookieID{
    _cookieID = [cookieID copy];
    [CTCUserModel saveInfo];
    
    [NetManager refreshCookieId:cookieID];
    [SDWebImageTool refreshCookieId:cookieID];
}
//
//- (void)setName:(NSString *)name
//{
//    _name = [name copy];
//    [CTCUserModel saveInfo];
//}
//
//- (void)setCity:(NSString *)city
//{
//    _city = [city copy];
//    [CTCUserModel saveInfo];
//}
//
//- (void)setHeadImg:(NSString *)headImg
//{
//    _headImg = [headImg copy];
//    [CTCUserModel saveInfo];
//}
//
//- (void)setUuid:(NSString *)uuid
//{
//    _uuid = [uuid copy];
//    [CTCUserModel saveInfo];
//}

- (void)setDetailModel:(CTCUserDetailModel *)detailModel
{
    _detailModel = detailModel;
    [CTCUserModel saveInfo];
}


@end
